<template>
  <div>
    <!--金卡协议-->
    <p class="text" v-html="goods.agreement"></p>
    <div class="divider">
      <p class="btn">
        请在下方签名
        <button @click="undo">回退</button>
        <button @click="clear()">清空</button>
      </p>
    </div>
    <VueSignaturePad width="100%" height="250px" ref="signaturePad"/>
    <p class="offer" v-if="showType" @click="goPay()">同意并支付</p>
  </div>
</template>

<script type="text/ecmascript-6">
  import {getVipcardDetail, saveSign} from '../../../../test/unit/http'
  import {Toast} from 'we-vue'

  export default {
    name: 'jinkaxieyisign',
    props: {},
    data() {
      return {
        goods: {},
        showType: true
      };
    },
    methods: {
      getVipcardDetailData(params) {
        getVipcardDetail(params).then(res => {
          console.log(res);
          if (res.code == 100) {
            this.goods = res.object;
          } else {
            Toast.fail(res.info);
          }
        })
      },
      undo() {
        this.$refs.signaturePad.undoSignature();
      },
      clear() {
        this.$refs.signaturePad.clearSignature();
      },
      goPay() {
        const {isEmpty, data} = this.$refs.signaturePad.saveSignature();
        console.log(isEmpty);
        console.log(data);
        if (isEmpty) {
          Toast.text({duration: 3000, message: '请先滑至屏幕最下方进行签名确认'});
          return;
        }
        let params = {
          goodsId: this.goods.goodsId,
          signStr: data
        };
        saveSign(params).then(res => {
          console.log(res);
          if (res.code == 100) {
            let accountElesignId = res.object.accountElesignId;
            if (this.$myUtils.isNull(accountElesignId)) {
              Toast.text({duration: 3000, message: '请先滑至屏幕最下方进行签名确认'});
              return;
            }
            this.$router.push({
              path: 'submitorder',
              query: {goodsId: this.goods.goodsId, accountElesignId: accountElesignId}
            })
          } else {
            Toast.text(res.info)
          }
        })
      }
    },
    created() {
      let goodsId = this.$route.query.goodsId;
      let showType = this.$route.query.showType;
      this.showType = showType;
      this.getVipcardDetailData({goodsId: goodsId});
    },
    components: {}
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="less" rel="stylesheet/less">
  .divider {
    background-color: #eee;
    height: 20px;
    width: 100%;
    text-align: left;
    padding: 3px;

    .btn {
      button {
        color: #fff;
        border: none;
        width: calc(35% - 50px);
        margin-left: 30px;
      }

      :last-child {
        background-color: #f9964e;

      }

      :first-child {
        background-color: #ccc;
        margin-right: 20px;
      }
    }
  }

  .title {
    font-size: 20px;
    color: #111;
    font-weight: 700;
    padding: 10px;
  }

  .text {
    font-size: 15px;
    color: #666;
    line-height: 25px;
    text-indent: 30px;
    text-align: left;
    padding: 10px 10px 60px;
  }

  .offer {
    position: fixed;
    bottom: 0;
    width: 100%;
    background-image: linear-gradient(to right, #f9964e, #ff8551);
    height: 50px;
    line-height: 50px;
    color: #fff;
  }
</style>
